#coding: utf-8 
import numpy as np
import math

# Create random input and output data
# 建立样本
f = np.linspace(-math.pi, math.pi, 2000)
y = np.sin(f)
#import pdb
#pdb.set_trace()
# Randomly initialize weights
# 随机的初始化权重
#  f -> y 
#   w1 + w2*f +w3*f^2 +w4 *f^3=y 
w1 = np.random.randn()
w2 = np.random.randn()
w3 = np.random.randn()
w4 = np.random.randn()
# lr =0.000001
learning_rate = 1e-6
for t in range(4000):
    # Forward pass: compute predicted y
    # y = w1 + w2 f + w3 f^2 + w4 f^3
    y_pred = w1 + w2 * f + w3 * f ** 2 + w4 * f ** 3
    import pdb
    #pdb.set_trace()
    # Compute and print loss
    # sum square error:SSE 
    loss = np.square(y_pred - y).sum()
    if t % 100 == 99:
        print(t, loss)

    # Backprop to compute gradients of w1, w2, w3, w4 with respect to loss
    # 计算权重的梯度以用于反向传播
    import pdb
    grad_y_pred = 2.0 * (y_pred - y)
    grad_w1 = grad_y_pred.sum()
    grad_w2 = (grad_y_pred * f).sum()
    grad_w3 = (grad_y_pred * f ** 2).sum()
    grad_w4 = (grad_y_pred * f ** 3).sum()

    # Update weights
    # 更新权重
    #a = w1 =learning-rate * grad_a
    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * grad_w2
    w3 -= learning_rate * grad_w3
    w4 -= learning_rate * grad_w4

print(f'Result: y = {w1} + {w2} f + {w3} f^2 + {w4} f^3')
